package org.dllwh.mysql.controller;


import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.annotation.Resource;
import org.dllwh.mysql.service.UserService;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName jasyptController
 * @Description 实现对数据库中敏感信息的加密解密
 * @Date 2025-09-23 10:07
 * @版本 V 1.0.1
 * @since JDK 17
 */
@RestController
public class JasyptController {
    @Resource
    private StringEncryptor stringEncryptor;

    /**
     * 加密解密接口
     *
     * @param message   加解密的内容
     * @param isEncrypt true 表示加密、false 表示解密
     */
    @RequestMapping("/jasypt/encryptor")
    public ObjectNode encrypt(@RequestParam(name = "message") String message,
                              @RequestParam(name = "isEncrypt") boolean isEncrypt) {
        JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
        String encrypt = isEncrypt ? stringEncryptor.encrypt(message) : stringEncryptor.decrypt(message);
        ObjectNode objectNode = nodeFactory.objectNode();
        objectNode.put("code", 200);
        objectNode.put("data", encrypt);
        return objectNode;
    }
}
